package com.bw.visitor_module.visitor

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.bw.visitor_module.visitor.entity.CarEntity
import com.bw.visitor_module.visitor.entity.VisitorEntity
import com.kf.common.mvi.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class VisitorViewModel : BaseViewModel<VisitorModel, VisitorState>() {
    override fun createState(): VisitorState {
        return VisitorState()
    }

    override fun createModel(): VisitorModel {
        return VisitorModel()
    }

    //部门方法
    fun requestDept() {
        var dept = SPUtils.getInstance().getInt("dept", -1)
        var map = HashMap<String, Any>()
        map.put("departmentId", dept)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestDept(map)
                .flowOn(Dispatchers.IO)
                .collect {
                    mutableContainer.updateUiState {
                        copy(deptEntity = it)
                    }
                }
        }
    }

    //预约访问
    fun requestVisitorUnCar(visitorEntity: VisitorEntity) {
        var map = HashMap<String, Any>()
        map.put("userId", visitorEntity.userId!!)
        map.put("visitorDescribe", visitorEntity.visitorDescribe!!)
        map.put("visitorEnd", visitorEntity.visitorEnd!!)
        map.put("visitorStart", visitorEntity.visitorStart!!)
        map.put("visitorTime", visitorEntity.visitorTime!!)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestVisitor(map).flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        mutableContainer.updateUiState {
                            copy(visitorEntity = it)
                        }
                    }
                }
        }
    }

    //预约访问
    fun requestVisitor(visitorEntity: VisitorEntity) {
        var map = HashMap<String, Any>()
        map.put("carNum", visitorEntity.carNum!!)
        map.put("parkId", visitorEntity.parkId!!)
        map.put("userId", visitorEntity.userId!!)
        map.put("visitorDescribe", visitorEntity.visitorDescribe!!)
        map.put("visitorEnd", visitorEntity.visitorEnd!!)
        map.put("visitorStart", visitorEntity.visitorStart!!)
        map.put("visitorTime", visitorEntity.visitorTime!!)
        map.put("parkName", visitorEntity.parkName!!)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestVisitor(map).flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        mutableContainer.updateUiState {
                            copy(visitorEntity = it)
                        }
                    }
                }
        }
    }

    //添加车辆方法
    fun requestCar(carEntity: CarEntity, visitorEntity: VisitorEntity) {
        var map: HashMap<String, Any> = HashMap()
        map.put("carNum", carEntity.carNum)
        map.put("colorId", carEntity.colorId)
        map.put("parkId", carEntity.parkId)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestCar(map).flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        requestVisitor(visitorEntity)
                    }
                }
        }
    }
}